<template>
    <div class="box">
        <div class="tops">
            <van-search v-model="value" placeholder="搜索" shape="round" />
            <div class="title" v-if="!value"><span>已添加人员</span><img @click="pushDetail" src="img/tianjiarenyuan@2x.png" alt=""></div>
        </div>
        <div class="top" style="height:1.5rem"></div>
        <ul v-if="!value">
            <li v-for="data in list" :key="data.id">
                <van-swipe-cell>
                <van-cell :border="false" :title="data.userName" />
                <template #right>
                    <div class="button">
                        <button @click="roleTo(data)" class="blue">权限范围</button>
                   <button class="red" @click="delPeople(data)">删除</button>
                    </div>
                </template>
                </van-swipe-cell>
            </li>
        </ul>
        <ul v-else>
                <li v-for="data in filterList" :key="data.id">
                    <van-swipe-cell>
                    <van-cell :border="false" :title="data.userName" />
                    <template #right>
                        <div class="button">
                            <button @click="roleTo(data)" class="blue">权限范围</button>
                    <button class="red" @click="delPeople(data)">删除</button>
                        </div>
                    </template>
                    </van-swipe-cell>
            </li>
        </ul>
    </div>
</template>
<script>
import { Dialog } from 'vant';
export default {
    data() {
        return {
            value:'',
            id:'',
            list:[],
            courseName:''
        }
    },
    computed:{
        filterList(){
             return this.list.filter(values => {
                return values.userName.match(this.value)
            })
        }
    },
    mounted() {
        this.id = this.$route.query.id;
        this.courseName = this.$route.query.courseName;
        (async ()=>{
            let { data } = await this.get('/course/queryUserByCourseId',{courseId:this.id});
            this.list = data;
        })()
    },
    methods: {
        pushDetail(){
            this.$router.push({
                path:'/changepart',
                query:{
                    id:this.id,
                    courseName:this.courseName
                }
            })
        },
        roleTo(data){
            this.$router.push({
                path:'/role',
                query:{
                    courseId:this.id,
                    courseName:this.courseName,
                    userId:data.userId,
                    userName:data.userName
                }
            })
        },
        delPeople(data){
            console.log(data);
            Dialog.confirm({
            title: '提示',
            message: '确认要删除该人员吗？',
            })
            .then(() => {
                (async ()=>{
                let values = await this.get('/user/userInfoShow',{userId:localStorage.getItem('id')});
                let adminName = values.data.userName;
                let value = {
                    courseId:this.id,
                    courseName:this.courseName,
                    userId:data.userId,
                    adminId:localStorage.getItem('id'),
                    adminName,
                    userName:data.userName
                }
                let msg = await this.get('/course/deletePeopleByUserIdAndCoursseId',value);
                if(msg.code == 200){
                    this.Toast({
                        type:'success',
                        duration:1000,
                        message:'删除成功'
                    })
                     this.list = this.list.filter(item=>item.userId != data.userId)
                }
                })()
            })
            .catch(() => {
                // on cancel
            });
            
        }
    },
}
</script>
<style lang="scss" scoped>
    .box{
    min-height: 100%;
    background: #F7F9FA;
}
.tops{
    width: 100%;
    // height: 0.6rem;
    position: fixed;
    background: #F7F9FA;
    top: 0;
    z-index: 100;
}
.button{
    display: flex;
    height: 100%;
}
.blue{
    width: 1.5rem;
    background: #299BFF;
    color: #FFFFFF;
    font-family: PingFang SC;
    font-weight: bold;
    color: #FFFFFF;
    display: flex;
    font-size: 0.28rem;
    align-items: center;
    justify-content: center;
    border: none;
    height: 100%;
}
.red{
    border: none;
    height: 100%;
    width: 1rem;
    font-size: 0.28rem;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #F10000;
    color: #FFFFFF;
}
.title{
    display: flex;
    padding: 0.1rem 0.2rem;
    margin-top: 0.13rem;
    box-sizing: border-box;
    align-items: center;
    background: white;
    justify-content: space-between;
    span{
        
            font-size: 0.3rem;
            font-family: PingFang SC;
            font-weight: bold;
            color: #333333;
    }
    img{
        display: block;
        width: 0.44rem;
        height: 0.44rem;
    }
}
ul{
    background: white;
    li{
        border-bottom: 1px solid #eee;
    }
}
/deep/ .van-search{
    height: 0.6rem;
    background: #F7F9FA;
    margin-top: 0.1rem;
}
/deep/ .van-search__content{
    background: white;
}
</style>